{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get exact DEM coordinates for desired corners"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gdal\n",
    "import osr\n",
    "import numpy as np\n",
    "from pyproj import Proj, transform"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# X (lon) and Y (lat) are arrays which contain the exact coordinates of of the source .tif file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load info\n",
    "slope_path = \"/projects/erke2265/DEM/DEM_ANT_CS_20130901.tif\"\n",
    "Raster = gdal.Open(slope_path)\n",
    "width = Raster.RasterXSize\n",
    "height = Raster.RasterYSize\n",
    "gt = Raster.GetGeoTransform()\n",
    "elevation = Raster.ReadAsArray()\n",
    "\n",
    "# Get initial coordinates and spatial resolution\n",
    "x0 = gt[0]\n",
    "dx = gt[1]\n",
    "y0 = gt[3]\n",
    "dy = gt[5]\n",
    "\n",
    "# Calculate X and Y coordinates\n",
    "X = np.zeros(width); X[:] = np.nan\n",
    "Y = np.zeros(height); X[:] = np.nan\n",
    "\n",
    "for j in range(0, width): \n",
    "    X[j] = x0 + dx * (j)\n",
    "    \n",
    "for j in range(0, height): \n",
    "    Y[j] = y0 + dy * (j)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Upper Left: coordinate transform \n",
    "### The upper left X and Y coordinates in the DEM are printed below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X: -154000.0\n",
      "Y: 154000.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  return _prepare_from_string(\" \".join(pjargs))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:294: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  projstring = _prepare_from_string(\" \".join((projstring, projkwargs)))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  return _prepare_from_string(\" \".join(pjargs))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:294: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  projstring = _prepare_from_string(\" \".join((projstring, projkwargs)))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/ipykernel_launcher.py:6: DeprecationWarning: This function is deprecated. See: https://pyproj4.github.io/pyproj/stable/gotchas.html#upgrading-to-pyproj-2-from-pyproj-1\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "# Define upper left coordinate in lat/lon (WGS-84)\n",
    "lon_src, lat_src = [-45, -88]\n",
    "\n",
    "inProj = Proj(init='epsg:4326') # WGS-84\n",
    "outProj = Proj(init='epsg:3031') # South Polar stereo \n",
    "lon_tgt, lat_tgt = transform(inProj,outProj,lon_src,lat_src)\n",
    "\n",
    "# Get X and Y\n",
    "X_ind = np.argmin(np.abs(X - lon_tgt))\n",
    "print(\"X: \" + str(X[X_ind]))\n",
    "Y_ind = np.argmin(np.abs(Y - lat_tgt))\n",
    "print(\"Y: \" + str(Y[Y_ind]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lower right: coordinate transform \n",
    "### The lower right X and Y coordinates in the DEM are printed below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X: 154000.0\n",
      "Y: -154000.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  return _prepare_from_string(\" \".join(pjargs))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:294: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  projstring = _prepare_from_string(\" \".join((projstring, projkwargs)))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:53: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  return _prepare_from_string(\" \".join(pjargs))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/pyproj/crs/crs.py:294: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6\n",
      "  projstring = _prepare_from_string(\" \".join((projstring, projkwargs)))\n",
      "/projects/erke2265/miniconda/envs/alpine3d/lib/python3.6/site-packages/ipykernel_launcher.py:6: DeprecationWarning: This function is deprecated. See: https://pyproj4.github.io/pyproj/stable/gotchas.html#upgrading-to-pyproj-2-from-pyproj-1\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "# Define upper left coordinate in lat/lon (WGS-84)\n",
    "lon_src, lat_src = [135, -88]\n",
    "\n",
    "inProj = Proj(init='epsg:4326') # WGS-84\n",
    "outProj = Proj(init='epsg:3031') # South Polar stereo \n",
    "lon_tgt, lat_tgt = transform(inProj,outProj,lon_src,lat_src)\n",
    "\n",
    "# Get X and Y\n",
    "X_ind = np.argmin(np.abs(X - lon_tgt))\n",
    "print(\"X: \" + str(X[X_ind]))\n",
    "Y_ind = np.argmin(np.abs(Y - lat_tgt))\n",
    "print(\"Y: \" + str(Y[Y_ind]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "alpine3d",
   "language": "python",
   "name": "alpine3d"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
